#include "../include/ListTimer.hpp"

void sort_timer_list::add_timer(std::shared_ptr<util_time> timer)
{
    if(!timer)
    {
        return;
    }
    if(!head)
    {
        head = tail = timer;    // 设置头尾指针
        return;
    }
    // 只有当头指针存在，并且待插入的结点超时时间小于头节点时，才进行插入
    if(timer->exprie < head->exprie)
    {
        timer->next = head;
        head -> prev = timer;
        head = timer;
        return;
    }
    // 否则查找合适的节点进行插入
    add_timer(timer,head);
}

void sort_timer_list::adjust_timer(std::shared_ptr<util_time> timer)
{
    // 只针对增加超时时间
    if(!timer)
    {
        return;
    }
    std::shared_ptr<util_time> temp = timer->next;
    if(!temp || timer->exprie < temp->exprie)   // 节点是尾节点或者这次增加的超时时间没有超过下一个节点时，不进行修改
    {
        return;
    }
    if(timer == head)
    {
        head = head -> next;
        head -> prev = nullptr;
        timer->next = nullptr;
        add_timer(timer,head);
    }else
    {
        timer->next->prev = timer->prev;
        timer->prev->next = timer->next;
        add_timer(timer,timer->next);
    }
}

void sort_timer_list::del_timer(std::shared_ptr<util_time> timer)
{
    if(!timer)
    {
        return;
    }
    if(timer == head && timer == tail)
    {
        timer = nullptr;
        head = nullptr;
        tail = nullptr;
        return;
    }
    if(timer == head)
    {
        head = head -> next;
        head->prev = nullptr;
        timer = nullptr;
        return;
    }
    if(timer == tail)
    {
        tail = tail -> prev;
        tail -> next = nullptr;
        timer = nullptr;
        return;
    }
    timer->prev->next = timer->next;
    timer->next->prev = timer->prev;
    timer = nullptr;
    return;
}

void sort_timer_list::tick(void)
{
    if(!head)
    {
        return;
    }
    printf("time tick\n");
    time_t currentTime = time(NULL);
    std::shared_ptr<util_time> temp = head;
    while (temp)
    {
        if(currentTime < temp->exprie)
        {
            break;
        }
        temp->cb_func(temp->user_data);
        head = temp->next;
        if(head)
        {
            printf("will close client is [%d]\n",temp->user_data->sockfd);
            printf("next client is [%d]\n",head->user_data->sockfd);
            head->prev = nullptr;
        }
        temp = head;
    }
}

void sort_timer_list::add_timer(std::shared_ptr<util_time> timer,std::shared_ptr<util_time> last_head)
{
    std::shared_ptr<util_time> prev = last_head;
    std::shared_ptr<util_time> temp = prev->next;
    while(temp)
    {
        if(timer->exprie < temp->exprie)
        {
            prev->next = timer;
            timer->next = temp;
            temp->prev = timer;
            timer->prev = prev;
            break;
        }
        prev = temp;
        temp = temp->next;
    }
    if(!temp)
    {
        prev->next = timer;
        timer->prev = prev;
        timer->next = nullptr;
        tail = timer;
    }
    printf("head is [%d]\n",head->user_data->sockfd);
    printf("next is [%d]\n",head->next->user_data->sockfd);
}